package com.moinut.studentattendancemanager.view.student;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.moinut.studentattendancemanager.UserManager;
import com.moinut.studentattendancemanager.controller.ComboItemController;
import com.moinut.studentattendancemanager.controller.StudentController;
import com.moinut.studentattendancemanager.controller.callback.OnGetListListener;
import com.moinut.studentattendancemanager.controller.callback.OnGetListener;
import com.moinut.studentattendancemanager.controller.callback.OnUpdateListener;
import com.moinut.studentattendancemanager.model.vo.db.Student;
import com.moinut.studentattendancemanager.utils.WindowJumper;
import com.moinut.studentattendancemanager.view.BaseFrame;

/**
 * @author MOILING
 */
public class ChangeStudentInfoFrame extends BaseFrame<ChangeStudentInfoFrame.ChangeStudentInfoPanel> {
    private static final long serialVersionUID = 1L;

    private static final int DEFAULT_WIDTH = 350;
    private static final int DEFAULT_HEIGHT = 350;

    public ChangeStudentInfoFrame() {
        super("修改学生信息", DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    @Override
    protected ChangeStudentInfoPanel onSetContentPane() {
        return new ChangeStudentInfoPanel();
    }

    /* package */ class ChangeStudentInfoPanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;

        private JLabel mNameLabel, mIdLabel, mAcademyLabel, mMajorLabel, mEmailLabel, mPicTipLabel;
        private JTextField mNameTextField, mIdTextField, mMajorTextField, mEmailTextField;
        private JComboBox<String> mAcademyComboBox;
        private JLabel mPicLabel;
        private JButton mChangePicButton, mChangeButton, mBackButton;
        private JPanel mInfoPanel, mPicPanel, mButtonPanel, mTopPanel;

        /*package*/ ChangeStudentInfoPanel() {
            setLayout(new BorderLayout());
            initTopPanel();
            initButtonPanel();
            initData();
        }

        private void initData() {
            ComboItemController.getAllCollegeNames(new OnGetListListener<String>() {
                @Override
                public void onSuccess(List<String> names) {
                    for (String name : names) {
                        mAcademyComboBox.addItem(name);
                    }
                }

                @Override
                public void onError(String error) {
                    JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
            StudentController.getStudentById(UserManager.userId, new OnGetListener<Student>() {
                @Override
                public void onSuccess(Student student) {
                    mNameTextField.setText(student.getStudentName());
                    mIdTextField.setText(student.getStudentId());
                    mAcademyComboBox.setSelectedItem(student.getStudentCollege());
                    mMajorTextField.setText(student.getStudentMajor());
                    mEmailTextField.setText(student.getStudentEmail());
                }

                @Override
                public void onError(String error) {
                    JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
        }

        private void initTopPanel() {
            mTopPanel = new JPanel(null);
            initInfoPanel();
            initPicPanel();
            add(mTopPanel, BorderLayout.CENTER);
        }

        private void initButtonPanel() {
            mChangeButton = new JButton("确认修改");
            mBackButton = new JButton("返回上级菜单");

            mButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mButtonPanel.add(mChangeButton);
            mButtonPanel.add(mBackButton);

            add(mButtonPanel, BorderLayout.SOUTH);

            mBackButton.addActionListener(this);
            mChangeButton.addActionListener(this);
        }

        private void initPicPanel() {
            mPicTipLabel = new JLabel("个人照片");
            mPicLabel = new JLabel();
            mPicLabel.setSize(140, 140);
            ImageIcon image = new ImageIcon(this.getClass().getResource("/res/images/portrait.jpg"));
            image.setImage(image.getImage().getScaledInstance(mPicLabel.getWidth(),
                    mPicLabel.getHeight(),
                    Image.SCALE_DEFAULT));
            mPicLabel.setIcon(image);
            mChangePicButton = new JButton("修改个人照片");

            mPicPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mPicPanel.setBounds(200, 0, 140, 300);

            mPicPanel.add(mPicTipLabel);
            mPicPanel.add(mPicLabel);
            mPicPanel.add(mChangePicButton);

            mTopPanel.add(mPicPanel);
        }

        private void initInfoPanel() {
            mNameLabel = new JLabel("姓名");
            mIdLabel = new JLabel("学号");
            mAcademyLabel = new JLabel("所属学院");
            mMajorLabel = new JLabel("所属专业");
            mEmailLabel = new JLabel("电子邮箱");
            mNameTextField = new JTextField(12);
            mNameTextField.setEditable(true);
            mIdTextField = new JTextField(12);
            mIdTextField.setEditable(true);
            mAcademyComboBox = new JComboBox<>();
            mMajorTextField = new JTextField(12);
            mMajorTextField.setEditable(true);
            mEmailTextField = new JTextField(12);
            mEmailTextField.setEditable(true);

            mInfoPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            mInfoPanel.setBounds(5, 5, 160, 340);

            mInfoPanel.add(mNameLabel);
            mInfoPanel.add(mNameTextField);
            mInfoPanel.add(mIdLabel);
            mInfoPanel.add(mIdTextField);
            mInfoPanel.add(mAcademyLabel);
            mInfoPanel.add(mAcademyComboBox);
            mInfoPanel.add(mMajorLabel);
            mInfoPanel.add(mMajorTextField);
            mInfoPanel.add(mEmailLabel);
            mInfoPanel.add(mEmailTextField);

            mTopPanel.add(mInfoPanel);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(mBackButton)) {
                WindowJumper.back(ChangeStudentInfoFrame.this);

            } else if (e.getSource().equals(mChangeButton)) {
                //updateStudentInfo(); 学生居然不能改……愤怒
                JOptionPane.showMessageDialog(null, "只有管理员才可以修改", "错误", JOptionPane.ERROR_MESSAGE);

            } else if (e.getSource().equals(mChangePicButton)) {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileFilter(new FileNameExtensionFilter("image files", "jpg", "jpeg", "gif", "png", "bmp"));
                chooser.setCurrentDirectory(new File("."));
                int result = chooser.showOpenDialog(ChangeStudentInfoFrame.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    String fileName = chooser.getSelectedFile().getPath();
                    ImageIcon image = new ImageIcon(fileName);
                    image.setImage(image.getImage().getScaledInstance(mPicLabel.getWidth(),
                            mPicLabel.getHeight(),
                            Image.SCALE_DEFAULT));
                    mPicLabel.setIcon(image);
                }
            }
        }

        private void updateStudentInfo() {
            Student student = new Student(mIdTextField.getText(), mNameTextField.getText(),
                    mAcademyComboBox.getItemAt(mAcademyComboBox.getSelectedIndex()), mMajorTextField.getText(), mEmailTextField.getText());
            StudentController.updateStudentInfo(UserManager.userId, student, new OnUpdateListener() {
                @Override
                public void onSuccess(String success) {
                    JOptionPane.showMessageDialog(null, success, "成功", JOptionPane.INFORMATION_MESSAGE);
                }

                @Override
                public void onError(String error) {
                    JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
        }
    }
}